<template>
  <div class="comment">
    <!-- comment Item -->
    <div class="card card-body mb-3">
      <div class="row">
        <div class="col-md-2">
          <a>
            <img
              class="rounded-circle d-nonse d-md-block ml-4"
              style="width:100px"
              :src="comment.avatar"
            >
          </a>
          <br>
          <p class="text-center">{{comment.name}}</p>
        </div>
        <div class="col-md-10">
          <p class="lead">{{comment.text}}</p>

          <!-- 判断是否可以删除留言 -->
          <button
            v-if="user != null && user.id == comment.user"
            type="button"
            class="btn btn-danger mr-1"
            @click="deleteComment(comment._id)"
          >
            <i class="fas fa-times"/>
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
export default {
  data() {
    return {};
  },
  computed: {
    user() {
      return this.$store.getters.user;
    }
  },
  methods: {
    deleteComment(id) {
      this.$axios
        .delete(`http://localhost:5000/api/posts/comment/${this.postId}/${id}`)
        .then(res => {
          //删除成功  更新数据
          this.$emit("update");
        })
        .catch(err => {
          console.log(err.response.data);
        });
    }
  },
  props: {
    comment: Object,
    postId: String
  },
  components: {},
  beforeRouteEnter(to, from, next) {
    vm => {
      console.log(this.comment);
    };
  }
};
</script>

<style scoped lang="stylus">
</style>
